#include "tube.h"

CTube::CTube() : CObjects()
{}

CTube::CTube(int Stand,int Length,int CanGetDown, int having, float x,float y)
{
	reality = 2;
	stand = Stand;
	length = Length;
	canGetDown = CanGetDown;
	pos_x = x;
	pos_y = y;
	if(stand == 0)
	{
		width = (length+1)*DIS;
		height = 32;
	}
	else
	{
		width = 32;
		height = (length+1)*DIS;
	}
	indexOfTree = ArrayIndex;
	ArrayObj[ArrayIndex++] = new CTree(x+8,y-(Length+1)*DIS);
	cTree->Add(ArrayObj[indexOfTree],indexOfTree);
	ArrayObj[indexOfTree]->life = having;
}

CTube::~CTube(){}

void CTube::RenderPieceTube(int type,float pos_x, float pos_y)
{
	if(type == 1)
		StaticPic->Render(_BackBuffer,pos_x,pos_y,2,0);
	else if(type == 2)
		StaticPic->Render(_BackBuffer,pos_x,pos_y,2,1);
	else if(type == 3)
		StaticPic->Render(_BackBuffer,pos_x,pos_y,3,0);
	else if(type == 4)
		StaticPic->Render(_BackBuffer,pos_x,pos_y,3,1);
	else if(type == 5)
		StaticPic->Render(_BackBuffer,pos_x,pos_y,4,4);
	else if(type == 6)
		StaticPic->Render(_BackBuffer,pos_x,pos_y,4,5);
	else if(type == 7)
		StaticPic->Render(_BackBuffer,pos_x,pos_y,4,6);
	else if(type == 8)
		StaticPic->Render(_BackBuffer,pos_x,pos_y,5,4);
	else if(type == 9)
		StaticPic->Render(_BackBuffer,pos_x,pos_y,5,5);
	else if(type == 10)
		StaticPic->Render(_BackBuffer,pos_x,pos_y,5,6);
}

void CTube::RenderFrame(LPDIRECT3DDEVICE9 d3ddv, int t)
{
	SpriteHandlerSP->Begin(D3DXSPRITE_ALPHABLEND);
	if(ArrayObj[indexOfTree]->life > 0)
		ArrayObj[indexOfTree]->RenderFrame(d3ddv,t);
	int l = 0;
	if(stand == 0)
	{
		do
		{
			RenderPieceTube(6,pos_x+(l+1)*DIS,(pos_y)-DIS);
			RenderPieceTube(9,pos_x+(l+1)*DIS,pos_y);
			l++;
		}while (l < length);
		RenderPieceTube(5,pos_x,(pos_y)-DIS);
		RenderPieceTube(8,pos_x,(pos_y));
	}
	else if (stand == 1)
	{
		do
		{
			RenderPieceTube(3,pos_x,pos_y-l*DIS);
			RenderPieceTube(4,pos_x+DIS,pos_y-l*DIS);
			l++;
		}while (l < length);
		RenderPieceTube(1,pos_x,pos_y-(length)*DIS);
		RenderPieceTube(2,pos_x+DIS,pos_y-(length)*DIS);
	}
	else 
	{
		RenderPieceTube(7,pos_x,pos_y-DIS);
		RenderPieceTube(10,pos_x,pos_y);
		RenderPieceTube(4,pos_x+DIS,pos_y-DIS);
		RenderPieceTube(4,pos_x+DIS,pos_y);
	}
	SpriteHandlerSP->End();
}